package com.wuming.caiwu.controller;

import com.wuming.caiwu.core.BusinessException;
import com.wuming.caiwu.core.CommonResult;
import com.wuming.caiwu.core.JsonResult;
import com.wuming.caiwu.model.Member;
import com.wuming.caiwu.model.SysRole;
import com.wuming.caiwu.service.MemberService;
import com.wuming.caiwu.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hanfei on 16/4/19.
 */
@Controller
@RequestMapping("/web/sysrole")
public class SysRoleController extends BaseController {

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private MemberService memberService;

    @RequestMapping(value = "/manageSysRole", method = RequestMethod.GET)
    public String manageSysRole() {
        return "/sysrole/manageSysRole";
    }

    @RequestMapping(value = "/addSysRole", method = RequestMethod.GET)
    public String addSysRole() {
        return "/sysrole/editSysRole";
    }

    @RequestMapping(value = "/setPermission/{roleId}", method = RequestMethod.GET)
    public String manageSysRole(@PathVariable Long roleId,Model model) {
        model.addAttribute("roleId",roleId);
        return "/sysrole/setPermission";
    }

    @ResponseBody
    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    public CommonResult getAll(){
        try {
            List<SysRole> roles = sysRoleService.getAllRoles();
            return CommonResult.createSuccessResult(roles, roles.size(), "查询角色信息成功.");
        } catch (BusinessException be) {
            logger.error("发生业务异常,异常信息:{}", be.getMessage());
            return CommonResult.createFailureResult("查询角色信息失败.");
        } catch (Exception e) {
            logger.error("发生系统异常,异常信息:{}", e.getMessage());
            return CommonResult.createFailureResult("查询角色信息失败.");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult list(@RequestParam(required = false) String name,@RequestParam(required = false) Integer storeId,
                             @RequestParam(defaultValue = "0") int pageIndex, @RequestParam(defaultValue = "10") int pageSize){
        try {
            Member current = memberService.getMemberById(storeId);
            List<SysRole> students = null;
            int total = 0;
            if(memberService.isAdministrator(current)){
                students = sysRoleService.getSysRolesByPage(name,null,pageIndex, pageSize);
                total = sysRoleService.getTotal(name,null);
            }else{
                students = sysRoleService.getSysRolesByPage(name,storeId,pageIndex, pageSize);
                total = sysRoleService.getTotal(name,storeId);
            }
            return CommonResult.createSuccessResult(students, total, "查询商品信息成功.");
        } catch (BusinessException be) {
            logger.error("发生业务异常,异常信息:{}", be.getMessage());
            return CommonResult.createFailureResult("查询商品信息失败.");
        } catch (Exception e) {
            logger.error("发生系统异常,异常信息:{}", e.getMessage());
            return CommonResult.createFailureResult("查询商品信息失败.");
        }
    }

    @RequestMapping(value = "/editSysRole/{id}", method = RequestMethod.GET)
    public String editStudent(Model model, @PathVariable Long id) {
        logger.info("准备修改商品信息ID:{}", id);
        SysRole product = sysRoleService.getSysRoleById(id);
        model.addAttribute("role", product);
        return "/sysrole/editSysRole";
    }

    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JsonResult save(SysRole product) {
        try {
            if (null != product.getId()) {
                sysRoleService.modifySysRole(product);
            } else {
                sysRoleService.saveSysRole(product);
            }
        } catch (Exception e) {
            logger.error("保存商品信息失败,失败信息:{}", e.getMessage());
            return new JsonResult<Object>(null, "保存商品基本信息失败", false);
        }
        return new JsonResult<Object>(product.getId(), "保存商品基本信息成功.", true);
    }

    @ResponseBody
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public CommonResult delete(@PathVariable Long id) {
        logger.info("准备删除角色信息ID:{}", id);
        try {
            sysRoleService.removeSysRoleById(id);
            return CommonResult.createSuccessResult("删除角色信息成功.");
        } catch (BusinessException be) {
            logger.error("发生业务异常,异常信息:{}", be.getMessage());
            return CommonResult.createFailureResult("删除角色信息失败.");
        } catch (Exception e) {
            logger.error("发生系统异常,异常信息:{}", e.getMessage());
            return CommonResult.createFailureResult("删除角色信息失败.");
        }
    }

}
